package ms.unicorn.modellanguage.providers;

import ms.unicorn.Element;
import ms.unicorn.diagram.providers.UnicornEditPartProvider;
import ms.unicorn.modellanguage.IModelLanguage;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageRegistry;

import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;

public class EditPartProvider extends UnicornEditPartProvider {

	@Override
	protected IGraphicalEditPart createEditPart(View view) {
		EditPart part = null;
		if (view.getElement() instanceof Element) {
			Element e = (Element) view.getElement();
			
			// Element can be null if view is a compartment view, for example
			// label view of an entity. In this case, suppose that parent edit part
			// will contain the link to the element.
			
			/*EditPart parent = context;
			while (parent != null && element == null) {
				Object model = parent.getModel();
				if (model instanceof View) 
					element = (Element) ((View) model).getElement();
				parent = parent.getParent();
			}*/
			
			LanguageElement langElement = LanguageRegistry.INSTANCE.getLanguageElement(e);
			if (langElement != null)
				part = langElement.getEditHelperAdvice().createEditPart(null, view);
		}
		if (! (part instanceof IGraphicalEditPart))
			part = super.createEditPart(view);
		return (IGraphicalEditPart) part;
	}
}
